﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SocialKit.LightRest;

namespace LightRestDemos.Compact
{
    public partial class UploaderForm : Form
    {
        RestClient client;

        public UploaderForm()
        {
            InitializeComponent();

            client = new RestClient();
            client.SendAsyncCompleted += client_SendAsyncCompleted;
        }

        void client_SendAsyncCompleted(object sender, SendAsyncCompletedEventArgs e)
        {
            this.Invoke((Action)(() =>
            {
                ToggleUIEnable(true);
                AppendStatus("Response received.");

                if (e.Error != null)
                {
                    AppendStatus("Error: " + e.Error.Message);
                }
                else
                {
                    tabResponse.Text = "Response #" + e.UserState;

                    AppendStatus("POST " + e.Response.ResponseUri + " " + (int)e.Response.StatusCode + " " + e.Response.StatusCode);
                    AppendStatus("Loading content...(" + e.Response.ContentLength + ")");

                    textBody.Text = e.Response.ReadContentAsString();

                    AppendStatus("Done.");
                }

            }), null);
        }

        private void menuClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void browseFileButton_Click(object sender, EventArgs e)
        {
            var fileDialog = new OpenFileDialog();
            fileDialog.Filter = "JPEG Images(*.jpg)|*.jpg|All Files(*.*)|*.*";

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                textFile.Text = fileDialog.FileName;
            }
        }

        int currentRequest = 0;

        private void uploadButton_Click(object sender, EventArgs e)
        {
            var file = textFile.Text;

            if (string.IsNullOrEmpty(file))
            {
                MessageBox.Show("Please select a file first.");
                return;
            }

            var uri = comboUri.Text;

            Uri address;

            if (!Uri.TryCreate(uri, UriKind.Absolute, out address))
            {
                MessageBox.Show("Uri is not well formed!");
                return;
            }

            textResponse.Text = "";
            textBody.Text = "";

            currentRequest++;
            ToggleUIEnable(false);
            this.tabUploader.SelectedIndex = 1;

            AppendStatus("Preparing file...");

            var fileForm = new HttpMultipartMimeForm().AppendFile("myfile", file).AppendValue("uploaded_at", DateTime.Now.ToString());

            AppendStatus("Preparing request...");

            var request = new HttpRequestMessage("POST", new Uri(comboUri.Text), fileForm);

            AppendStatus("Connecting...");

            client.SendAsync(request, currentRequest);
        }

        private void ToggleUIEnable(bool enabled)
        {
            this.browseFileButton.Enabled = enabled;
            this.uploadButton.Enabled = enabled;
        }

        private void AppendStatus(string message)
        {
            textResponse.Text += Environment.NewLine + message;
        }
    }
}